package com.zxp.utils.configParseUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesConfigReader {

    private static final String CONFIG_FILE = "config.properties";

    private static Properties properties;

    static {
        loadProperties();
    }

    /**
     * Load properties from the specified configuration file.
     * This method initializes the 'properties' object by loading key-value pairs from the configuration file.
     * If the configuration file is not found, a RuntimeException is thrown.
     *
     * @throws RuntimeException If there is an error loading the configuration file.
     */
    private static void loadProperties() {
        properties = new Properties();
        try (InputStream input = PropertiesConfigReader.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
            if (input == null) {
                throw new RuntimeException("Unable to find " + CONFIG_FILE);
            }
            properties.load(input);
        } catch (IOException e) {
            throw new RuntimeException("Error loading " + CONFIG_FILE, e);
        }
    }
    /**
     * Get the value for a specified key.
     *
     * @param key The key to get the value for
     * @return The value for the specified key
     */

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

    public static void main(String[] args) {
        // Example usage:
        String databaseUrl = getProperty("database.url");
        String username = getProperty("database.username");
        String password = getProperty("database.password.zxp");
        System.out.println("Database URL: " + databaseUrl);
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);
    }
}
